{% extends "base.html" %}

{% block title %}评分类型管理{% endblock %}

{% block content %}
<div class="container mt-4">
    <div class="card">
        <div class="card-header">
            <h4>评分类型管理</h4>
        </div>
        <div class="card-body">
            <div class="alert alert-info">
                <strong>评分类型规则：</strong>
                <ul>
                    <li>每个班级默认使用字母等级（A/B/C/D/E）</li>
                    <li>第一次评分时确定评分类型，之后自动锁定</li>
                    <li>同一班级的所有作业必须使用相同的评分类型</li>
                </ul>
            </div>

            <table class="table">
                <thead>
                    <tr>
                        <th>班级标识</th>
                        <th>当前评分类型</th>
                        <th>状态</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    {% for config in configs %}
                    <tr>
                        <td>{{ config.class_identifier }}</td>
                        <td>{{ config.get_grade_type_display }}</td>
                        <td>
                            {% if config.is_locked %}
                                <span class="badge bg-success">已锁定</span>
                            {% else %}
                                <span class="badge bg-warning">未锁定</span>
                            {% endif %}
                        </td>
                        <td>
                            {% if not config.is_locked %}
                                <button class="btn btn-sm btn-primary" onclick="changeGradeType('{{ config.class_identifier }}')">
                                    修改
                                </button>
                            {% else %}
                                <button class="btn btn-sm btn-secondary" disabled>已锁定</button>
                            {% endif %}
                        </td>
                    </tr>
                    {% empty %}
                    <tr>
                        <td colspan="4" class="text-center">暂无评分类型配置</td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
</div>

<script>
function changeGradeType(classIdentifier) {
    const newGradeType = prompt('请选择新的评分类型：\n1. letter - 字母等级 (A/B/C/D/E)\n2. text - 文本等级 (优秀/良好/中等/及格/不及格)\n3. numeric - 数字等级 (90-100/80-89/70-79/60-69/0-59)');

    if (!newGradeType) return;

    if (!confirm(`确定要将班级 "${classIdentifier}" 的评分类型修改为 "${newGradeType}" 吗？`)) {
        return;
    }

    fetch('/grading/change-grade-type/', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'X-CSRFToken': '{{ csrf_token }}'
        },
        body: `class_identifier=${encodeURIComponent(classIdentifier)}&new_grade_type=${encodeURIComponent(newGradeType)}`
    })
    .then(response => response.json())
    .then(data => {
        if (data.status === 'success') {
            alert(`修改成功！${data.message}`);
            location.reload();
        } else {
            alert(`修改失败：${data.message}`);
        }
    })
    .catch(error => {
        console.error('Error:', error);
        alert('请求失败，请重试');
    });
}
</script>
{% endblock %}
